package com.xueyu.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.ProductDao;
import com.xueyu.dao.ProductsDao;
import com.xueyu.domain.Products.ProductsBean;
import com.xueyu.domain.SupplierBean;
import com.xueyu.domain.drug.drug;
import com.xueyu.domain.drugs.drugsBean;
import com.xueyu.domain.goods.Product;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
@RestController
@RequestMapping("/Product")
//接待登记
public class ProductController {
    @Autowired
    ProductDao ProductDao;
    @Autowired
    ProductsDao ProductsDao;
    @Autowired
    com.xueyu.dao.SupplierDao SupplierDao;

    @Autowired
    com.xueyu.dao.drugsDao drugsDao;


    @RequestMapping("fp")
    public ResultBean fp() {
        ResultBean resultBean = new ResultBean();
        List<ProductsBean> f =  ProductsDao.selectList(null);
        QueryWrapper<SupplierBean> queryWrapper = new QueryWrapper<>();
        // 设置查询条件
        queryWrapper.eq("suppliers", 1);  // 根据字段相等进行查询
        List<SupplierBean> g =  SupplierDao.selectList(queryWrapper);
        resultBean.put("f",f);
        resultBean.put("g",g);
        return resultBean;
    }
    @RequestMapping(value = "/add")
    public  ResultBean add(HttpServletRequest request, Product Product, @RequestPart(value = "pic", required = false) MultipartFile[] pic) throws Exception{
        ResultBean resultBean = new ResultBean();
        if(pic!=null) {
            for (MultipartFile file : pic) {
                String picName = UUID.randomUUID().toString();
                // 截取文件的扩展名(如.jpg)-
                String oriName = file.getOriginalFilename();
                String extName = oriName.substring(oriName.lastIndexOf("."));
                String path = request.getSession().getServletContext().getRealPath("\\");
                //日期处理
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String sdate = sdf.format(date);
                String dirName = "\\upload\\" + sdate + "\\";
                File dirFile = new File(path + dirName);
                if (!dirFile.exists()) {
                    dirFile.mkdirs();
                }
                String newFileName = picName + extName;
                File targetFile = new File(path + dirName, newFileName);
                file.transferTo(targetFile);
                String path1 = dirName + newFileName;
                System.out.println("path=" + path1);


                Product.setBrand(path1);
            }
        }
        if(Product.getProdid()==0) {
            Date date = new Date();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = sdf1.format(date);
            Product.setCtime(currentTime);
            Product.setGtime(currentTime);
            ProductDao.insert(Product);
        }else {
            Date date = new Date();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = sdf1.format(date);
            Product.setGtime(currentTime);
            ProductDao.updateById(Product);
        }
        return resultBean;
    }

    //删除
    @RequestMapping("/del/{prodId}")
    public ResultBean del(@PathVariable("prodId") int prodId){
        ProductDao.deleteById(prodId);
        return new ResultBean();
    }
    //
//
//
    @RequestMapping("/getid/{prodId}")
    public Map<String, Object> getid(@PathVariable("prodId") int prodId){
        ResultBean resultBean = new ResultBean();
        if (prodId>0) {
            QueryWrapper<Product> queryWrapper = Wrappers.query();
            queryWrapper.eq("prodId", prodId);
            List<Product> ReceptionList = ProductDao.selectList(queryWrapper);
            resultBean.put("data", ReceptionList);
        }
        return resultBean;
    }
    //分页.条件查询.
    @RequestMapping("/fy")
    public ResultBean fy( @RequestBody  Map map) {
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        Page<Product> page = new Page<>(current, size);
        QueryWrapper<Product> queryWrapper = createSchoolQueryWrapper(map);
        page = ProductDao.selectPage(page, queryWrapper);
        List<Product> list = page.getRecords();
        for (Product Drug : list) {
            // 获取药品的分类id
            Long categoryId = Long.valueOf(Drug.getStyle());
            Long categoryIds = Long.valueOf(Drug.getSupplierid());
            // 根据分类id查询对应的分类信息
            ProductsBean category = ProductsDao.selectById(categoryId);
            SupplierBean category2 = SupplierDao.selectById(categoryIds);
            category2.setSuppliers(String.valueOf(1)); // 添加查询条件
            // 如果分类存在，则将分类名称设置到药品表中
            if (category != null) {
                Drug.setP(category.getProductName());
            }
            if (category2 != null) {
                Drug.setG(category2.getSupplierName());
            }
        }
        ResultBean resultBean = new ResultBean();
        resultBean.put("page",page);
        resultBean.put("list",list);
        return resultBean;
    }
    //
    @RequestMapping("/zd")
    public ResultBean zd(@RequestBody  Map map)
    {
        QueryWrapper<Product> queryWrapper = createSchoolQueryWrapper(map);
        List<Product> list = ProductDao.selectList(queryWrapper);
        ResultBean resultBean = new ResultBean();
        resultBean.put("list",list);
        return resultBean;
    }
    //
    //封装好的查询条件
    public QueryWrapper<Product> createSchoolQueryWrapper(Map map) {
        String prodId =  (String)map.get("prodid");
        String prodname =  (String)map.get("prodname");
        String g =  (String)map.get("g");
        String drugF =  (String)map.get("drugF");
        QueryWrapper<Product> queryWrapper = Wrappers.query();
        if (prodId != null && !"".equals(prodId)) {
            queryWrapper.eq("prodId", prodId);
        }
        if (g != null && !"".equals(g)) {
            queryWrapper.eq("supplierId", g);
        }
        if (drugF != null && !"".equals(drugF)) {
            queryWrapper.eq("style", drugF);
        }
        if (prodname != null && !"".equals(prodname)) {
            queryWrapper.like("prodname", "%" + prodname + "%");
        }
        return queryWrapper;
    }
}




